local ProximityPromptService = game:GetService("ProximityPromptService")
local IsClient = game:GetService("RunService"):IsClient();
local M={}
if IsClient then
     M = _G.ClientOHOH.class.new(script, _G.ClientOHOH.files.Client_PlayerComponent());
else
    M = _G.ServerOHOH.class.new(script,  _G.ServerOHOH.files.Server_PlayerComponent());
end

local Buff_Cfg=_G.CommonOHOH.config.Buff
local ItemTool=_G.CommonOHOH.load.ItemTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.BuffList={} --buff 列表{[Buff对象]=true}
    self.BuffIdList={} --buffId{[Id]=buff对象}
    if IsClient then
        self:ClientInit()
    else
        self:ServerInit()
    end 
end;

function M.ServerInit(self)
  
end

function M.ClientInit(self)

end

function M.CreateBuff(self,buffId) -- 已经存在的buff，直接加时间
    local cfg=Buff_Cfg[buffId]
    local buff=self.BuffIdList[cfg.id]
    if buff then --已经存在buff，直接加时间
        buff:Add(cfg.time)
    else
        local buffClass= require(script:WaitForChild(cfg.className))
        buff= buffClass.new(self.parent,cfg.time,cfg) --传cfg根据具体情况使用数据
        buff.DestroyEvent:Connect(function(obj)
            self.BuffList[obj]=nil
            self.BuffIdList[cfg.id]=nil
        end)
        self.BuffList[buff]=true
        self.BuffIdList[cfg.id]=buff
    end
    return buff
end



function M.UseBuff(self,buffId)
    local DataStore=self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
    local count= ItemTool.GetItemCount(buffId,DataStore)
    if count and count>0 then
        self:CreateBuff(buffId)
        ItemTool.SaveData(buffId,DataStore,-1)	
        DataStore:Execut(function()
        end);	
        self.parent:SynchDataToClient("buff")
        return true
    end
    return false
end

function M.dtor(self)
    for k,v in pairs(self.BuffList) do
        k:dtor()
    end
	M.super.dtor(self);
end;
if not IsClient then
    local function_list = _G.ServerOHOH.function_list;
    M:RegistFunction(function_list.use_buff,M.UseBuff)
end

return M